package com.tsyun.view_project;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.util.Timer;

public class OtherViewActivity extends Activity {

    Button btn;
    int progress = 0;
    private WebView webView;

    private TextView tvTitle;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.otherview2_activity);

        ProgressBar pb = findViewById(R.id.pb);
        Timer timer = new Timer();
        SeekBar seekBar = findViewById(R.id.seekbar);
        tvTitle = findViewById(R.id.tv_title);

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//                Toast.makeText(OtherViewActivity.this,"当前进度"+progress,Toast.LENGTH_LONG).show();
                Log.i("progress", String.valueOf(progress));
                OtherViewActivity.this.progress = progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        webView = findViewById(R.id.wv);
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);
         new AlertDialog.Builder(this)
                 .setNegativeButton("取消", (dialog, which) -> {

                 })
                 .setPositiveButton("确认",null)
                 .setNeutralButton("中间",null)
//                 .setMessage("这里是弹出框")
                 .setCancelable(false)
                 .setView(R.layout.dialog_demo)
//                 .setTitle("提示")
                 .show();

        webView.loadUrl("https://www.sina.com.cn");
        webView.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                pb.setProgress(newProgress);
                if (newProgress==100){
                    pb.setVisibility(View.GONE);
                }
            }

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                tvTitle.setText(title);

            }
        });
        webView.setWebViewClient(new WebViewClient(){

        });

        ImageView ivSample = findViewById(R.id.iv_sample);
        //设置图片源resource
//        ivSample.setImageResource(R.drawable.bg);
//        ivSample.setImageDrawable(getResources().getDrawable(R.drawable.cat));
//        ivSample.setImageDrawable(new ColorDrawable(Color.BLUE));
        ivSample.setImageBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.cat));
//        ivSample.setImageURI(Uri.parse("file:///storage/0/Android/"));
//        ivSample.setScaleType(ImageView.ScaleType.);
        //加载本地图片
//        ivSample.setImageBitmap(BitmapFactory.decodeFile();
        //通过图片流加载图片
//        ivSample.setImageBitmap(BitmapFactory.decodeStream());
//        TimerTask task = new TimerTask() {
//            @Override
//            public void run() {
//                progress++;
//                pb.setProgress(progress);
//            }
//        };
        //在子线程中可以更新UI，但是前提是不能有其他耗时操作，如果有其它耗时操作之后，会崩溃
//        Thread thread = new Thread(){
//            @Override
//            public void run() {
//                super.run();
//                try {
//                    sleep(1000);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//                pb.setProgress(progress);
//            }
//        };
//        thread.start();
//        timer.schedule(task,0,1000);
//        EditText etSearch = findViewById(R.id.et_search);
//        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
//            @Override
//            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//                if (actionId== EditorInfo.IME_ACTION_SEARCH){
////                    Toast.makeText(OtherViewActivity.this,"您点击了搜索",Toast.LENGTH_SHORT).show();
////                    return true;
//                }
//                return false;
//            }
//        });

//        etSearch.setOnKeyListener(new View.OnKeyListener() {
//            @Override
//            public boolean onKey(View v, int keyCode, KeyEvent event) {
////                if (keyCode==KeyEvent.KEYCODE_ENTER){
////                    Toast.makeText(OtherViewActivity.this,"您点击了搜索",Toast.LENGTH_SHORT).show();
////                }
//                return false;
//            }
//        });
//        btn = findViewById(R.id.btn_1);
//        //设置点击事件2
//        btn.setOnClickListener(v->{
//            Toast.makeText(this,"点击了我111111",Toast.LENGTH_SHORT).show();
//        });
    }


//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode==KeyEvent.KEYCODE_ENTER){
//            Toast.makeText(OtherViewActivity.this,"您点击了搜索",Toast.LENGTH_SHORT).show();
//        }
////        if (keyCode==KeyEvent.KEYCODE_BACK){
////            Toast.makeText(OtherViewActivity.this,"您点击了返回",Toast.LENGTH_SHORT).show();
////            return true;
////        }
//
//        return false;
//    }

    @Override
    public void onBackPressed() {
        if (webView!=null){
            if (webView.canGoBack()){
                webView.goBack();
            }else {
                finish();
            }
        }else {
            super.onBackPressed();
            finish();
        }
    }

    //设置点击事件1
    public void onClickMe(View view) {
        Toast.makeText(this, "点击了我", Toast.LENGTH_SHORT).show();
    }


}
